/**
 *  This code is a part of Quadratix project.
 * Copyright (C) 2009 - 2010 Valery A. Ilychev aka Sarutobi (ilychev at gmail dot com)
 * 
 *  This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package ru.sarutobi.quadratix;


/**
 *  Board representation class.
 * @author valera
 *
 */
public class Board {
	public static final int EMPTY = 0;
	public static final int WHITE = 1;
	public static final int BLACK = 2;

	/** Board representation array */
	private int[] board;
	
	/** Current board size */
	private int size;
	
	
	private int last;

	/**
	 *  Constructor for board
	 * @param size size of required board. Must be odd!
	 * TODO check that size is odd!
	 */
	public Board(int size) {
		this.size = size;
		board = new int[size * size];
		last = size * size;
		initialise();
	}

	public void initialise() {
		for (int x = 0; x < size * size; x++)
				board[x] = EMPTY;
	}

	public boolean Move(int x, int y, int occup) {
		if ((x < 0) || (x >= size) || (y < 0) || (y >= size)
				|| (occup == EMPTY))
			return false;
		if (EMPTY != getPosition(x, y))
			return false;
		setPosition(x, y, occup);
		last--;
		return true;
	}

	public boolean finished(){
		return (0 == last);
	}
	
	public int getSize() {
		return size;
	}
	

	public int getPosition(int x, int y){
		return board[x + y * size];
	}
	
	public int getPosition(int pos){
		return board[pos];
	}
	
	private void setPosition(int x, int y, int occup){
		board[x + y * size] = occup;
	}

	public int[] getBoard() {
		return board;
	}
}
